/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SMS;

import RMS.RememberLoadedLevel;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

/**
 *
 * @author SamSung
 */
public class SMS {

    private static MessageConnection con;
    public static boolean isSend = false;
    public static int n;

    public static void sendMessage(final String numb, final String mes, int level) {
//        n = 0;
//        while (n < 20) {
        try {
            con = (MessageConnection) Connector.open(numb);
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (SecurityException se) {
            se.printStackTrace();
            System.out.println("con " + se.toString());
        }
        TextMessage msg = (TextMessage) con.newMessage(con.TEXT_MESSAGE);
        msg.setPayloadText(mes);
        msg.setAddress(numb);
        try {
            con.send(msg);
            if (level > 0) {
                RememberLoadedLevel.createRecord(level);
            }
        } catch (SecurityException se) {
            se.printStackTrace();
            System.out.println("send " + se.toString());
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
//                n++;
//                if (n < 20) {
//                    try {
//                        Thread.sleep(3000);
//                    } catch (InterruptedException ex) {
//                        ex.printStackTrace();
//                    }
//                }
        }
//            System.out.println("n=" + n);
    }
}
